local skynet = require "skynet"
local util = require "util"

local math_random = math.random
local table_insert = table.insert
local table_remove = table.remove
local PI_ANGLE = (314 - 214) / 2

local fishing_robot ={}
local LOCK_TIME_MIN = 40
local LOCK_TIME_MAX = 60

local M = {}
local MT = {
    __index = fishing_robot
}

function M.new(uid,player_info,robot_type,addr,fd,multiple)
    local curr_time = util.get_now_time()
    local o = {
        uid = uid,
        player_info = player_info,
        addr = addr,
        fd = fd,
        robot_type = robot_type,
        bullet_id       = 0,
        turn_angle_time = curr_time + math_random(4,6),
        angle     =  (math_random(1,PI_ANGLE) - PI_ANGLE) / 100,
        multiple = multiple,

        --机器人停顿
        pause_interval_begin_time = 0 ,--开始停顿时间
        pause_interval_time = 0,    --停顿的时间
        -- pause_time = 0,             --停顿时间
        --火炮切换时间
        fire_interval_time = curr_time + math_random(10,20),
        --离开
        leave_interval_time = curr_time + math_random(300,600),
        --锁定鱼
        lock_interval_time = curr_time + math_random(10,20),
        lock_durative_time = 0,
        lock_fish_id = 0,
        lock_status = false,
        unreal_lock_status = false,
        lock_open = true,

        lock_fish = {},
        unreal_lock_fish_list = {},
        unreal_lock_need_times = 0,
        unreal_lock_cur_times  = 0,
        unreal_lock_durative_time = 0,
        unreal_lock_fish_id = 0,

        target_fire_power = 1,
        pause_end_time = 0,
        on_exiting = false,
    }
    print_r(o)
    return setmetatable(o, MT)
end

function fishing_robot:get_bullet_id()
    if self.bullet_id >= 65535 then
        self.bullet_id = 0
    end

    self.bullet_id = self.bullet_id + 1
    return self.bullet_id
end

function fishing_robot:on_register(uid,bullet_manager)
    if uid ~= self.uid then
        return
    end

    self.bullet_manager = bullet_manager
    skynet.send(self.addr,'lua','enter',self.uid,self.fd)
end


function fishing_robot:on_update(bullet_manager)
    self.bullet_manager = bullet_manager
end

function fishing_robot:update_status(uid,coins,bullet_manager)
    if uid ~= self.uid then
        return
    end

    --self.player_info.coins = coins
    --self.bullet_manager = bullet_manager
end

function fishing_robot:add_lock_fish(fish_id,curr_msec,score_rate)
    if score_rate >= 50 then --50倍以上鱼有概率锁定,
        print("add_lock_fish===111",fish_id,score_rate,self.uid,self.lock_status)
        local robot_info = _ENV.global_configs.robot[self.robot_type]
        if not robot_info then
            return
        end

        local trigger_rate = 0
        for k,v in pairs(robot_info.fire_lock) do
            if v.score_rate == score_rate then
                trigger_rate = v.trigger_rate
                break
            end
        end

        local now_sec = util.get_now_time()
        local rate = math_random(1,100)
        if rate <= trigger_rate and not self.lock_status then --触发锁定
            self.lock_fish = {fish_id = fish_id,begin_lock_time = now_sec + 2}
        end
    end

    --假锁定
    if score_rate >= 10 and score_rate < 50 then
        table_insert(self.unreal_lock_fish_list,{fish_id = fish_id,time = math.floor(curr_msec / 1000)})
    end
end

function fishing_robot:open_close_lock(is_open)
    self.lock_open = is_open
end

function fishing_robot:on_start()
    
end

function fishing_robot:on_game_over()
    
    local curr_time = util.get_now_time()
    if curr_time - self.start_time > self.live_time then
        self:set_exit()
    end

    if is_exit() then
        self:on_leave()
    end
end

function fishing_robot:is_fish_exist(fish_id)
    local ret = skynet.call(self.addr,'lua','is_fish_exist',self.uid,self.fd,{fish_id = fish_id})
    print("is_fish_exist========",fish_id,ret)

    return ret
end

function fishing_robot:pause_interval()
    dbglog("pause_interval")
    -- if math_random(1,100) <= 50 then
        local curr_time = util.get_now_time()
        self.pause_interval_begin_time = curr_time + math_random(1,2)
        self.pause_interval_time = self.pause_interval_begin_time + math_random(3,8)
        dbglog(self.pause_interval_begin_time,self.pause_interval_time)
    -- end
end

function fishing_robot:update_play(now_sec,now_usec)
    if self:is_exit() then
        return
    end

    if self.on_exiting then
        dbglog("robot_on_exiting=====",self.uid)
        return
    end

    local bullet_manager = self.bullet_manager
    if not bullet_manager then
        print("fishing_robot--->bullet_manager is nil",self.uid)
        return
    end
--    print("update_play======1111")
    
    local robot_info = _ENV.global_configs.robot[self.robot_type]
    if not robot_info then
        return
    end

    --暂停射击
    if self.pause_interval_begin_time < now_sec and self.pause_interval_time > now_sec then
        dbglog(self.pause_interval_time,now_sec)
        return
    end

    --子弹威力切换
    if self.fire_interval_time < now_sec and robot_info.fire_change.gun_rate then
        -- 设置下次切换威力的时间
        local interval_time = robot_info.fire_change.interval_time or {}
        self.fire_interval_time = now_sec + math_random(interval_time[1] or 0, interval_time[2] or 0)
        self.pause_end_time = now_sec + math_random(5,10)
        local total_rate = 0
        for k,v in pairs(robot_info.fire_change.gun_rate) do
            total_rate = total_rate + v.trigger_rate
            if math_random(1,100) <= total_rate then
                self.target_fire_power = v.power_rate
                break
            end
        end
    end
  --  print("update_play======2222",self.fire_interval_time,now_sec,self.pause_end_time,self.target_fire_power)

    local curr_power = self.player_info.curr_power or 0
    if self.fire_interval_time >= now_sec then
        if self.target_fire_power ~= curr_power then
             local power_status = self.target_fire_power >= curr_power and 1 or 2
      --      print("fire_interval_time=========",curr_power,self.target_fire_power)
            print(self.uid,"-----robot-REQ_CHANGE_POWER--------",power_status)
            skynet.send(self.addr,'lua','REQ_CHANGE_POWER',self.uid,{power_status = power_status},self.fd)
        end

        if not self.lock_status and now_sec <= self.pause_end_time then
        --    print("update_play======0000",self.lock_status,now_sec,self.pause_end_time,now_sec <= self.pause_end_time)
            return
        end
    end

    --print("update_play======333")
    
    --真锁定判断
    if not self.lock_status and next(self.lock_fish) and now_sec >= self.lock_fish.begin_lock_time then
        skynet.send(self.addr,'lua','REQ_LOCK_FISH',self.uid,{fish_id = self.lock_fish.fish_id},self.fd)
        self.lock_durative_time = now_sec + math_random(LOCK_TIME_MIN,LOCK_TIME_MAX)
        self.lock_status = true
    end

    --取消真锁定
    if self.lock_status and (now_sec >= self.lock_durative_time or not self:is_fish_exist(self.lock_fish.fish_id)) then
--        print(self.uid,"-----robot-REQ_UNLOCK_FISH--------")
        self.lock_status = false
        skynet.send(self.addr,'lua','REQ_UNLOCK_FISH',self.uid,{},self.fd)
        self.turn_angle_time = 0
    end

   -- print("update_play======444")
    --假锁定判断
    if (not self.lock_status) and (not self.unreal_lock_status) and next(self.unreal_lock_fish_list) then
        for _,lock_info in pairs(self.unreal_lock_fish_list) do
            local fish_sec = lock_info.time
            if (fish_sec + 20 > now_sec and fish_sec + 2 < now_sec) and self:is_fish_exist(lock_info.fish_id) then
                skynet.send(self.addr,'lua','on_unreal_lock_fish',self.uid,self.fd,{fish_id = lock_info.fish_id})
                self.unreal_lock_fish_id = lock_info.fish_id
                self.unreal_lock_status = true
                self.unreal_lock_need_times = math_random(10,30)
                self.unreal_lock_durative_time = now_sec + math_random(LOCK_TIME_MIN,LOCK_TIME_MAX)
            end
        end
    end
    --取消假锁定
    if self.unreal_lock_status and not self.lock_status then
        local need_times = self.unreal_lock_need_times
        local fish_id = self.unreal_lock_fish_id
        if self.unreal_lock_durative_time < now_sec or need_times <= self.unreal_lock_cur_times then
            print(self.uid,"-----robot-REQ_CANCEL_UNREAL_LOCK_FISH--------")
            self.unreal_lock_status = false
            self.unreal_lock_need_times = 0
            self.unreal_lock_cur_times = 0
            self.unreal_lock_durative_time = 0
            self.unreal_lock_fish_id = 0
            skynet.send(self.addr,'lua','on_cancel_unreal_lock_fish',self.uid,self.fd,{})
        end
    end

    --先检查钱
    local player_info = self.player_info
    local power = player_info.curr_power or 0
    local pwoer_coins = power * self.multiple
    local user_bullet = bullet_manager[self.uid]
--    print(self.uid,"fishing_robot--->fire count is max")
    if user_bullet and #user_bullet >= global_configs.fishing_config.max_bulllet then
        print(self.uid,"fishing_robot--->fire count is max")
        --print_r(user_bullet)
        return
    end

    -- -- 开火角度
    -- if now_sec > self.turn_angle_time then
    --     local tem_ang = math_random(0,PI_ANGLE)
    --     if math_random(1,2) == 1 then
    --         tem_ang = tem_ang - PI_ANGLE
    --     end
    --     self.angle  =  tem_ang / 100
    --     print(self.uid,"fishing_robot--->turn_angle_time",self.angle)
    --     self.turn_angle_time = util.get_now_time() + math_random(5,10)
    -- end

    --print("update_play======666")
    self.angle  = 0.1 --(0,pi)
    local bullet_id = self:get_bullet_id()
    local msg = {angle = self.angle,bullet_id = bullet_id}
    player_info.coins = player_info.coins - pwoer_coins
    skynet.send(self.addr,'lua','fire',self.uid,self.fd,msg)

    self.unreal_lock_cur_times = self.unreal_lock_status and (self.unreal_lock_cur_times + 1) or self.unreal_lock_cur_times
    for index,lock_info in pairs(self.unreal_lock_fish_list) do
        local fish_sec = lock_info.time
        if fish_sec + 30 < now_sec then
            table_remove(self.unreal_lock_fish_list,index)
   --         print(self.uid,"fishing_robot--->unlock_fish_list",fish_sec,now_sec)
            break
        end 
    end
end

function fishing_robot:close_server()
    self:on_leave()
end

function fishing_robot:on_ticked_out() --被踢掉
    print("on_ticked_out================")
    self:on_leave()
end

function fishing_robot:set_exit()
    self.exit = true
end

function fishing_robot:is_exit()
    return self.exit
end

function fishing_robot:on_leave()
    skynet.send(self.addr,'lua','REQ_LEAVE',self.uid,{},self.fd)
    self.on_exiting = true
    print(self.uid,"fishing_robot--->on_leave")
end

function fishing_robot:get_curr_guns_fire_interval()
    local ret = 30

   for _,guns_info in pairs(global_configs.fishing_guns) do
        if tonumber(guns_info.id) == self.player_info.curr_guns then
            ret = guns_info.fire_interval
            break
        end
    end
    ret = ret < 10 and 10 or ret
    return ret
end

function fishing_robot:update()
    while not self:is_exit() do
        local curr_time = util.get_now_time()
        
        self:update_play(curr_time)
        --print(self.uid,"fishing_robot--->update")
        local update_time = self:get_curr_guns_fire_interval()
        skynet.sleep(update_time)
    end

    dbglog(self.uid,'exit !!!!')
end

function fishing_robot:on_exit()
    dbglog("robot_on_exit====",self.uid)
    self:set_exit()
    R().robotmgr(1):send('.msg_handler','put_back_robots',{{type=self.robot_type,uid=self.uid,coins=self.player_info.coins}})
end


return M